package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @title: SetmealController
 * @Author 椎名真白
 * @Date: 2022-04-30 10:04
 * @Version 1.0
 */


@Slf4j
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;

    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        //一:找出Page<Setmeal>  此时页面中没有分类名称 需要进一步处理
        //构造分页器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //条件选择器
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
    /*    if(name!=null){
            queryWrapper.like("name",name);
        }*/
        queryWrapper.like(name != null, "name", name);
        //queryWrapper.eq("status",1);
        queryWrapper.orderByDesc("update_time");
        setmealService.page(pageInfo, queryWrapper);

        //二：解决没有分类名称问题
        Page<SetmealDto> pageInfo2 = new Page<>(page, pageSize);
        //拷贝total 等分页参数

        BeanUtils.copyProperties(pageInfo, pageInfo2, "records");

        ArrayList<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : pageInfo.getRecords()) {
            //将每一个setmeal转换为SetmealDto
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            setmealDto.setCategoryName(categoryService.getById(setmealDto.getCategoryId()).getName());
            setmealDtoList.add(setmealDto);
        }

        pageInfo2.setRecords(setmealDtoList);
        return R.success(pageInfo2);
    }

    /**
     * @description: 新增套餐 使用增强的方法 因为保存套餐的时候要讲餐品信息也进行保存
     * @author: 椎名真白
     * @date: 2022-04-30 10:51
     * @param: setmealDto
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/
    @CacheEvict(value = "setmealCache", allEntries = true) //清除setmealCache名称下,所有的缓存数据
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("新增成功");
    }

    @CacheEvict(value = "setmealCache", allEntries = true) //清除setmealCache名称下,所有的缓存数据
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * @description: 修改套餐的信息
     * @author: 椎名真白
     * @date: 2022-04-30 14:53
     * @param: setmealDto
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.upadteWithSetmealDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * @description: 根据id查询菜品信息 进行回显
     * @author: 椎名真白
     * @date: 2022-04-30 14:53
     * @param: id
     * @return: com.itheima.reggie.common.R<com.itheima.reggie.dto.SetmealDto>
     **/
    @GetMapping("/{id}")
    public R<SetmealDto> updateById(@PathVariable Long id) {

        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //将setmeal的数据拷贝到setmealDto
        BeanUtils.copyProperties(setmeal, setmealDto);

        //获取条件选择器
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id", id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }

    /**
     * @description: 修改套餐的停售起售状态
     * @author: 椎名真白
     * @date: 2022-04-30 15:05
     * @param: status
     * @param: ids
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/
   /* @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,Long ids){
        QueryWrapper<Setmeal> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",ids);
        Setmeal setmeal = setmealService.getOne(queryWrapper);
        setmeal.setStatus(status);
        setmealService.updateById(setmeal);
        return R.success("停售成功");
    }*/
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        //QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        //queryWrapper.in("id", ids);
        for (int i = 0; i < ids.length; i++) {
            Setmeal setmeal = setmealService.getById(ids[i]);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        //Setmeal setmeal = setmealService.getOne(queryWrapper);
       /* QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        for (int i = 0; i < ids.size(); i++) {
            queryWrapper.eq("id",ids.get(i));
            Setmeal setmeal = setmealService.getOne(queryWrapper);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }*/
        return R.success("停售成功");
    }


    @GetMapping("list")
    @Cacheable(value = "setmealCache", key = "#categoryId", unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        queryWrapper.eq("status", status);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    @GetMapping("dish/{id}")
    public R<List<String>> dish(@PathVariable Long id) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        setmealService.getOne(queryWrapper);
/*
        QueryWrapper<SetmealDish> setmealDishQueryWrapper=new QueryWrapper<>();
        setmealDishQueryWrapper.eq("setmeal_id",id);
        SetmealDish setmealDish = setmealDishService.getById(id);
        List<SetmealDish> setmealDishes=new ArrayList<>();*/
        return R.success(null);
    }
}
